﻿using System.Collections.Generic;
using ICTBilling;
using System.Configuration;
using System;
using System.Linq;
using ICTBilling;

public partial class ExceptionManagement_ExceptionHandler : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (Session["ExceptionHandler"] != null)
            {
                List<ExceptionHandler> ExecutionCollection = (List<ExceptionHandler>)Session["ExceptionHandler"];
                tbExceptionHandler _tbExceptionHandler = new tbExceptionHandler();
                using (ICTBillingEntities _ICTBillingEntities = new ICTBillingEntities())
                {
                    foreach (ExceptionHandler exceptionHandler in ExecutionCollection)
                    {
                        try
                        {
                            if (exceptionHandler.ExceptionType == ICTBilling.ExceptionType.UnHandeled ||
                                exceptionHandler.ExceptionType == ICTBilling.ExceptionType.PageUrl)
                            {
                                string InnerException = exceptionHandler.Exception.InnerException.Message.ToString();
                                if (InnerException.Contains("\'"))
                                    InnerException = InnerException.Substring(0, InnerException.IndexOf('\''));

                                if (_ICTBillingEntities.tbExceptionHandlers.Where(item => item.InnerException.Contains(InnerException)).ToList().Count < 1)
                                    InnerException = "Fatal Error";
                                _tbExceptionHandler = _ICTBillingEntities.tbExceptionHandlers.Where(item => item.InnerException.Contains(InnerException)).First();
                            }
                            else
                            {
                                string InnerException = exceptionHandler.ExceptionType.ToString();
                                if (_ICTBillingEntities.tbExceptionHandlers.Where(c => c.InnerException == InnerException) == null)
                                    InnerException = "Fatal Error";
                                _tbExceptionHandler = _ICTBillingEntities.tbExceptionHandlers.Where(c => c.InnerException == InnerException).First();
                                _tbExceptionHandler.Message = string.Format(_tbExceptionHandler.Message, exceptionHandler.ParameterTitle);
                                _tbExceptionHandler.HelpNote = string.Format(_tbExceptionHandler.HelpNote, exceptionHandler.ParameterTitle);
                            }
                            ExceptionHandlerBulletedList.Items.Add(string.Format(@"{0} : {1}. {2}. \n <div dir=""ltr""> {3} </div>", _tbExceptionHandler.Title, _tbExceptionHandler.Message, _tbExceptionHandler.HelpNote,_tbExceptionHandler.InnerException.ToString()));
                        }
                        catch
                        {
                            ExceptionHandlerBulletedList.Items.Add(string.Format("{0} : {1}. {2}", ConfigurationManager.AppSettings["DefaultExceptionTitle"], ConfigurationManager.AppSettings["DefaultExceptionMessage"], ConfigurationManager.AppSettings["DefaultExceptionHelpNote"]));
                        }
                    }
                }
                Session["ExceptionHandler"] = null;
            }
        }
        catch
        {
            Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "CloseForm", "window.close();", true);
        }
    }
}
